<?php


namespace app\common\validate;


class RCBCValidate extends CommonValidate
{
    protected $rule = [
        'group_id'        => 'require|integer|between:1,100000000',
        'group_name'      => 'require|chsAlphaNum|length:1,16',
        'role_ids'        => 'roleIds',
        'role_id'         => 'require|integer|between:1,100000000',
        'role_name'       => 'require|chsAlphaNum|length:1,16',
        'role_app'        => 'alphaNum|length:1,30',
        'role_controller' => 'alphaNum|length:1,30',
        'role_action'     => 'alphaNum|length:1,30',
        'role_method'     => 'require|method',
        'role_is_menu'    => 'require|zeroOne',
        'role_common'     => 'require|zeroOne',
        'role_index'      => 'require|integer|between:0,100000000',
        'role_pid'        => 'require|integer|between:0,100000000',
    ];

    protected $message = [
        'group_id.require'         => '请输入 id',
        'group_id.integer'         => 'id 必须是整数',
        'group_id.between'         => 'id 格式错误',
        'group_name.require'       => '请输入角色名',
        'group_name.chsAlphaNum'   => '角色名必须是1-16个汉字、字母或数字',
        'group_name.length'        => '角色名必须是1-16个汉字、字母或数字',
        'role_ids.roleIds'         => 'role_ids 格式错误',
        'role_id.require'          => '请输入 role_id',
        'role_id.integer'          => 'role_id 格式错误',
        'role_id.between'          => 'role_id 格式错误',
        'role_name.require'        => '请输入 role_name',
        'role_name.chsAlphaNum'    => 'role_name 格式错误',
        'role_name.length'         => 'role_name 格式错误',
        'role_app.alphaNum'        => 'role_app 格式错误',
        'role_app.length'          => 'role_app 格式错误',
        'role_controller.alphaNum' => 'role_controller 格式错误',
        'role_controller.length'   => 'role_controller 格式错误',
        'role_action.alphaNum'     => 'role_controller 格式错误',
        'role_action.length'       => 'role_controller 格式错误',
        'role_method.require'      => '请输入 role_method',
        'role_method.method'       => 'role_method 格式错误',
        'role_is_menu.require'     => '请输入 role_is_menu',
        'role_is_menu.zeroOne'     => 'role_is_menu 格式错误',
        'role_common.require'      => '请输入 role_common',
        'role_common.zeroOne'      => 'role_common 格式错误',
        'role_index.require'       => '请输入 role_index',
        'role_index.integer'       => 'role_index 必须是整数',
        'role_index.between'       => 'role_index 格式错误',
        'role_pid.require'         => '请输入 role_pid',
        'role_pid.integer'         => 'role_pid 必须是整数',
        'role_pid.between'         => 'role_pid 格式错误',
    ];

    protected $scene = [
        'postRBACGroup'   => ['group_name', 'role_ids'],
        'putRBACGroup'    => ['group_id', 'group_name', 'role_ids'],
        'deleteRBACGroup' => ['group_id'],
        'postRBACRole'    => [
            'role_name',
            'role_app',
            'role_controller',
            'role_action',
            'role_method',
            'role_is_menu',
            'role_common',
            'role_index',
            'role_pid'
        ],
        'putRBACRole'     => [
            'role_id',
            'role_name',
            'role_app',
            'role_controller',
            'role_action',
            'role_method',
            'role_is_menu',
            'role_common',
            'role_index',
            'role_pid'
        ],
        'deleteRBACRole'  => ['role_id'],
    ];

    /**
     * 登录场景移除 user_name 唯一性
     * @return RCBCValidate
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/8/12
     * @Time: 17:35
     */
    //public function scenePutRbacGroup()
    //{
        //return $this->remove('group_name', 'unique');
    //}

    /**
     * @param $ids
     * @return bool|null
     *
     * @Author: Li Guo Xin <guoxinlee129@gmail.com>
     * @Date: 2020/7/7
     * @Time: 10:59
     */
    public function roleIds($ids): ?bool
    {
        try {
            $ids = explode(',', $ids);
            $ids = array_filter($ids);
            foreach ($ids as $id) {
                if (!filter_var($id, FILTER_VALIDATE_INT)) {
                    return false;
                }
            }
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * @param $method
     * @return bool
     */
    public function method($method): ?bool
    {
        return in_array($method, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'NULL']);
    }
    /**
     * @param $number
     * @return bool
     */
    public function zeroOne($number): ?bool
    {
        return in_array($number, ['1', '0'], true);
    }
}